/**
 * Created by huangying on 16/12/6.
 */
var price = 0;
var gold_num = 0;
var good_id = 0;
//全部商品列表页面
function redemption_list() {
    $.ajax({
        type: 'get',
        url: url_link + 'goods',
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var redemption_list_show = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    if (obj.data.total == 0) {
                        redemption_list_show += "<h2 class='none_info' style='margin: 163px 0;'>还没有商品上架哦</h2>"
                    } else {

                        $.each(obj.data.rows, function (i, item) {
                            redemption_list_show += "<li>" +
                                "<a href='redemption_details.html?good_id=" + item.good_id+"' target='_blank'>" +
                                "<img src=" + item.cover + ">" +
                                "<div class='productPrice-box'>所需金币：<span class='productPrice'>" + item.price + "</span></div>" +
                                "<div class='productName-box'>" +
                                "<span class='productName'>" + item.good_name + "</span>" +
                                "</div>" +
                                "</a>" +
                                "</li>"
                        });
                    }
                    $('.productGroup').html(redemption_list_show)
                }
            } catch (e) {

            }
        }
    });
}

//详情页面
function redemption_details() {
    $.ajax({
        type: 'get',
        url: url_link + 'goods',
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var redemption_list_details = "";
                price = obj.data.price;
                if (obj.code == 0) {
                    redemption_list_details += "<div class='layer1'>" +
                        "<div class='imglist'>" +
                        "<ul>" +
                        "<li>" +
                        "<img src=" + obj.data.cover + ">" +
                        "</li>" +
                        "</ul>" +
                        "</div>" +
                        "<div class='layer-text'>" +
                        "<div class='productName'>" + obj.data.good_name + "</div>" +
                        "<div class='productPrice'>兑换所需金币：<span class='good_price'>" + obj.data.price + "</span>" +
                        "<div class='balance'>现有金币：<span>" + gold_num + "</span></div>" +
                        "</div>" +
                        "<div class='queueInfo toLine' style='font-size: 14px;margin-top: 20px;'></div> " +
                        "<div class='last'> " +
                        "<div class='store'><span>库存：<span  class='good_total'>" + obj.data.remain_count + "</span></span></div>" +
                        "<div class='productNo'>" +
                        "<a href='javascript:void(0);' rel='nofollow' class='reduc'>-</a>" +
                        "<input type='text' value='1' class='good_num' onkeyup='good_num_change()'>" +
                        "<a rel='nofollow' class='add' href='javascript:void(0);'>+</a>" +
                        "</div>" +
                        "<a class='addCart_add'>立即兑换</a>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "<div class='layer999 mb20'>";
                    var _content_description = obj.data.good_description.split(",");
                    for (var _a = 0; _a < _content_description.length; _a++) {
                        redemption_list_details += "<div style='width: 950px;text-align: center;margin: 0 74px'><img src=" + _content_description[_a] + " style='width: 100%;'></div>"
                    }
                    redemption_list_details += "</div>";
                    $('.details_show').html(redemption_list_details)
                }
            } catch (e) {

            }
        }
    });
}

//详情页加减数量
function adddel() {
    $(".add").each(function () {
        $(this).click(function () {
            var $multi = 0;
            var vall = $(this).prev().val();
            if (vall < $('.good_total').html()) {
                vall++;
                $(this).prev().val(vall);
                $multi = parseFloat(vall) * price;
                $('.good_price').html($multi)
            } else {
                layer.msg("库存不足!请重新选择购买数量")
            }
        })

    });
    //减
    $(".reduc").each(function () {
        $(this).click(function () {
            var $multi1 = 0;
            var vall1 = $(this).next().val();
            vall1--;
            if (vall1 <= 0) {
                vall1 = 1;
            }
            $(this).next().val(vall1);
            $multi1 = parseFloat(vall1) * price;
            $('.good_price').html($multi1)
        })

    })
}


//直接输入购买量
function good_num_change() {
    var $multi = 0;
    var vall = $('.good_num').val();
    if (parseFloat(vall) <= $('.good_total').html()) {
        $multi = parseFloat(vall) * price;
        $('.good_price').html($multi)
    } else if (parseFloat(vall) > $('.good_total').html()) {
        layer.msg("库存不足!请重新选择购买数量", {time: 2000})
    }
}


//我的金币数量
function gold() {
    $.ajax({
        type: 'get',
        url: url_link + 'users/' + user_id,
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                gold_num = obj.data.gold;
            } catch (e) {

            }
        }
    });
}


//立即兑换
$(document).on('click', '.addCart_add', function () {
    var _count = $('.good_num').val();
    var _total = $('.good_price').html();
    if (_total > price) {
        layer.msg("您的金币不足!", {time: 2000})
    } else {
        layer.msg('您确定要花'+_total+'个金币,对该商品进行购买么', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: 'post',
                    url: url_link + 'exchanges',
                    async: false,
                    data: {
                        user_id: user_id,
                        good_id: good_id,
                        count: _count
                    },
                    success: function (data) {
                        try {
                            var obj = JSON.parse(data);
                            if (obj.code == 0) {
                                layer.msg('兑现成功!正在为您跳转到"兑换中心"页面...');
                                setTimeout(function () {
                                    window.location.href = "redemption_list.html"
                                }, 3000)
                            }
                        } catch (e) {
                            layer.msg(data.msg);
                        }
                    }
                });

            }
        });

    }
});
